package com.seari.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.seari.bean.Professionxdk;
import com.seari.bean.RightLineGuardPoint;
import com.seari.mapper.RightLineGuardPointMapper;
import com.seari.pojo.LineInfoXdk;
import com.seari.service.LocationService;
import com.seari.service.RightLineGuardPointService;
import com.seari.utils.GuavaCacheUtil;
import com.seari.utils.HandlerNullUtils;

@Service("rightLineGuardPointService")
public class RightLineGuardPointServiceImpl implements RightLineGuardPointService{
	
	@Autowired
	private RightLineGuardPointMapper rightLineGuardPointMapper;
	
	@Autowired
	private LocationService locationService;
	
	@Override
	public List<Map<String, Object>> getGuardPointProfession(){
		
		List<Professionxdk> jqGzFlagList = (List<Professionxdk>) GuavaCacheUtil.get("professionxdkList");
		
		List<Professionxdk> ProfessionxdkList1 = new ArrayList<Professionxdk>();
		List<Professionxdk> ProfessionxdkList2 = new ArrayList<Professionxdk>();
		
		for(Professionxdk professionxdk:jqGzFlagList) {
			if(professionxdk.getSubId().equals("0")) {
				ProfessionxdkList1.add(professionxdk);
			}else {
				ProfessionxdkList2.add(professionxdk);
			}
		}
		
		List<Map<String, Object>> map1List =  new ArrayList<Map<String, Object>>();
		for(Professionxdk pro1:ProfessionxdkList1) {
			Map<String, Object> mapOne = new HashMap<String, Object>();
			List<Map<String, Object>> map2List = new ArrayList<Map<String, Object>>();
			for(Professionxdk pro2:ProfessionxdkList2) {
				if(pro1.getProfessionId().equals(pro2.getSubId())) {
                    Map<String, Object> mapTwo = new HashMap<String, Object>();
                    mapTwo.put("title",pro2.getProfessionName());
                    mapTwo.put("id",pro2.getProfessionId());
                    map2List.add(mapTwo);
				}
			}
            mapOne.put("title",pro1.getProfessionName());
            mapOne.put("id",pro1.getProfessionId());
            mapOne.put("children",map2List);
            map1List.add(mapOne);
		}
		
		return map1List;
	}
	
	
	@Override
	public List<String> getGuardPointLineInfo(){
		
		return LineInfoXdk.lineList;
	}
	
	
	@Override
	@DS("slave_1")
	public List<Professionxdk> getGuardPointProfessionFromMysql(){
		
		List<Professionxdk> list = rightLineGuardPointMapper.getGuardPointProfessionFromMysql();
		return list;
	}
	
	@Override
	@DS("master")
	public List<RightLineGuardPoint> getRightLineGuardPointInfoList(Map<String,Object> mapInMap){
		
		List<Professionxdk> professionxdkList = (List<Professionxdk>) GuavaCacheUtil.get("professionxdkList");

		if(!HandlerNullUtils.objectIsNull(mapInMap.get("zy"))) {
			List<String> listIn = new ArrayList<String>();
			String zyString = (String) mapInMap.get("zy");
			if(zyString.contains(",")) {
				String[] str = zyString.split(",");
				
				List<String> strList = new ArrayList<String>();
				for(Professionxdk professionxdk : professionxdkList) {
					if(!professionxdk.getSubId().equals("0")) {
						strList.add(professionxdk.getSubId());
					}
				}
				
				List<String> listInId = new ArrayList<String>();
				for(int i=0;i<str.length;i++){
					for(String st:strList) {
						if(str[i].equals(st)) {
							listInId.add(str[i]);
						}
					}
				}
				
				for(Professionxdk professionxdk : professionxdkList) {
					for(String stri:listInId) {
						if(professionxdk.getSubId().equals(stri)) {
							listIn.add(professionxdk.getProfessionName());
						}
					}
				}
			}
			mapInMap.put("listIn", listIn);
		}
		
		List<RightLineGuardPoint> list = rightLineGuardPointMapper.getRightLineGuardPointInfoList(mapInMap);
		
		if(!HandlerNullUtils.objectIsNull(list)) {
			locationService.fillLocationOfRightLineGuardPoint(list);
		}
		
		List<RightLineGuardPoint> listOut = new ArrayList<RightLineGuardPoint>();
		
		if(!HandlerNullUtils.objectIsNull(mapInMap.get("line"))) {
			String line = (String) mapInMap.get("line");
			String lineName = LineInfoXdk.lineInfoTreeMap.get(line);
			
			if(!HandlerNullUtils.objectIsNull(lineName)){
				for(RightLineGuardPoint r:list) {
					if(r.getLine().contains(lineName)) {
						listOut.add(r);
					}
				}
			}
			
			for(RightLineGuardPoint rg:listOut) {
				for(Professionxdk pro:professionxdkList) {
					if(pro.getProfessionName().equals(rg.getDutyPro())) {
						for(Professionxdk pros:professionxdkList) {
							if(pro.getSubId().equals(pros.getProfessionId())) {
								rg.setDutyBigPro(pros.getProfessionName());
							}
						}
					}
				}
			}
			
			return listOut;
		}else {
			
			for(RightLineGuardPoint rg:list) {
				for(Professionxdk pro:professionxdkList) {
					if(pro.getProfessionName().equals(rg.getDutyPro())) {
						for(Professionxdk pros:professionxdkList) {
							if(pro.getSubId().equals(pros.getProfessionId())) {
								rg.setDutyBigPro(pros.getProfessionName());
							}
						}
					}
				}
			}
			return list;
		}
	}
}
